/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.edit

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.orhanobut.logger.Logger
import com.tencent.map.geolocation.TencentLocation
import com.tencent.map.geolocation.TencentLocationManager
import com.tencent.map.geolocation.TencentLocationRequest
import kotlinx.android.synthetic.main.activity_publish.*
import kotlinx.android.synthetic.main.include_header.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.ApiManager
import top.stime21.myneighbours.base.BuzMvpActivity
import top.stime21.myneighbours.base.io_main
import top.stime21.myneighbours.base.showConfirmDialog
import top.stime21.myneighbours.base.showTipDialog
import top.stime21.myneighbours.data.entity.Need
import top.stime21.myneighbours.data.entity.Skill
import top.stime21.myneighbours.module.login.NeedContract
import top.stime21.myneighbours.module.login.presenter.NeedPrensenterImpl

class PublishActivity : BuzMvpActivity<NeedContract.Presenter>(), NeedContract.View {
    override fun onDetailsGetSuccess(data: Need?) {
    }
    override fun onFinishSuccess() {
    }

    override fun onMyNeedGetSuccess(data: List<Need>?) {
    }

    override fun onMyNeedMoreGetSuccess(data: List<Need>?) {
    }
    override fun onPublishSuccess() {
        showTipDialog("提示", "发布成功,请耐心等待其他人接受", View.OnClickListener {
            finish()
        })
    }
    override fun onAcceptSuccess() {
    }

    override fun getPresenter(): NeedContract.Presenter {
        return NeedPrensenterImpl(this)
    }

    override fun onCancleSuccess() {
    }

    override fun onNearbySuccess(data: List<Need>?) {
    }

    override fun onSearchSuccess(data: List<Need>?) {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_publish)
        init()
    }

    private var location: TencentLocation? = null

    fun init() {
         location = TencentLocationManager.getInstance(this).lastKnownLocation
        if (location != null) {
            tv_address.text = location!!.address
        }
        tv_title.text = "发布需求"
        btn_back.setOnClickListener({ finish() })
        btn_publish.setOnClickListener {
            var title  =tv_need_title.text.toString()
            var content =    tv_need_content.text.toString()
            if(TextUtils.isEmpty(title)){
                showToast("请输入标题")
                return@setOnClickListener
            }
            if(TextUtils.isEmpty(content)){
                showToast("请输入内容")
                return@setOnClickListener
            }
            if(location==null){
                TencentLocationManager.getInstance(this).requestLocationUpdates(TencentLocationRequest.create(),null)
                return@setOnClickListener
            }
            var type:Long = 1;
            if(spinner.selectedItem!=null){
                type =( spinner.selectedItem as Skill).id
            }
            showConfirmDialog("提示", "是否确定要发布?", View.OnClickListener {
                mPrensenter!!.publish(type.toInt(),location!!.address, location!!.latitude, location!!.longitude,content,title)
            })
        }
        skilladapter = SpinnerAdapter(this)
        spinner.adapter = skilladapter
        getSkills()
    }
     var skilladapter: SpinnerAdapter?=null

    fun getSkills(){
        ApiManager.ins().dataApi.skills().io_main().subscribe({
            r->
            if(r.isSuccess ){
                skilladapter!!.setDataUpdate( r!!.data)
            }
        },{
            t->
            Logger.e(t.toString())
        })

    }




}
